﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string StringConnection = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; //connection string to database
            SqlConnection Conn = new SqlConnection(StringConnection); //set connection string to sql connection
            Conn.Open(); //open connection to database
            Session["connection"] = Conn;
        }
    }

    protected void registerButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("RegisterControl.aspx"); //if user want to register, redirect to register page
    }
    protected void loginButton_Click1(object sender, EventArgs e)
    {
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            Request.Cookies.Clear();
            Session.Abandon();
            FormsAuthentication.SignOut();
        }

        if (Membership.ValidateUser(usernameBox.Text, passwordBox.Text)) //if username and password match a user
        {
            FormsAuthentication.SetAuthCookie(usernameBox.Text, true); //set authentication cookie to the username
            Response.Redirect("ContentPage.aspx"); //forward user to the content page
        }
        else
        {
            Response.Redirect("RegisterControl.aspx"); //if user dont exist, redirect to register page
        }   
    }

    public void logo_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Homescreen.aspx"); //click on the logo will redirect user to homescreen
    }
    
}
